<!--导入需求计划弹窗-->
<template>
	<el-dialog
		title="需求计划导入"
		:visible.sync="importDialogVisible"
		width="50%"
		:before-close="importHandleClose"
		v-dialogDrag
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<importFile
			:moduleName="moduleName"
			@setImportData="getImportData"
			:closeImportDialog="importDialogVisible"
			:tempUrl="tempUrl"
		></importFile>
		<span slot="footer" class="dialog-footer">
			<el-button @click="importHandleClose">取消</el-button>
			<el-button type="primary" @click="saveImportData" :loading="btn_loading">导入</el-button>
		</span>
	</el-dialog>
</template>

<script>
import importFile from "@/components/importFile";
import fileStream from "@/public/fileStream";
import Msg from "@/public/message";
import { maxDate, minDate } from "@/util/format";
/**
 * @group demandPlanList
 * 需求计划列表
 */
export default {
	name: "importDemand",
	data () {
		return {
			valitDate: "",
			moduleName: "demandPlan",
			tempUrl: "",
			btn_loading: false,
			importData: []
		};
	},
	props: {
		importDialogVisible: {
			type: Boolean,
			required: true,
		},
	},
	methods: {
		importHandleClose () {
			// 关闭弹窗方法
			// @arg 参数是布尔值
			this.$emit("closeDialog", false);
		},
		/**
		* @vuese
		* 获取导入数据
		*
		* @arg 参数为数组,代表导入的数据
		*/
		getImportData (val) {
			this.importData = val;
		},
		/**
		* @vuese
		* 确认保存导入的数据
		*/
		saveImportData () {
			if (Array.isArray(this.importData) && this.importData.length > 0) {
				let dates = this.importData.map((date) => {
					return Date.parse(date.startSdate);
				});
				let maxDates = maxDate(dates);
				let minDates = minDate(dates);
				let num = Math.floor((maxDates - minDates) / (24 * 3600 * 1000));
				if (num > 6) {
					Msg.showPopMsg("每次只能做一周内的计划,请重新导入!");
					return;
				}
				// 确认导入数据
				// @arg 参数是导入的数据
				this.$emit("saveImportData", this.importData);
				this.importData = []
			} else {
				Msg.showPopMsg("导入数据不能为空!");
			}
		},
		/**
		* @vuese
		* 获取导入模板
		*/
		getContractTemp () {
			fileStream.getPrintTempUrl("demandPlan").then((url) => {
				this.tempUrl = url;
			});
		},
		// 获取导入数据明细
		getImportDeliveryPlanData (planMxs) {
			this.$http
				.importDeliveryPlan(planMxs)
				.then(({ data }) => {
					if (data.code == 1) {
						data.rows.forEach((row) => {
							row.deliveryplanmxs.forEach((item) => {
								let amount = 0;
								item.deliveryplantms.forEach((el) => {
									amount += Number(el.planAmount);
								});
								item.planAmount = amount;
								item.confirmAmount = 0;
								item.state = 0;
							});
							row.type = 0; //0采购方计划
						});
						this.$emit("saveImportData", data.rows);
					} else {
						Msg.showErrorMsg(data.info);
					}
				})
				.catch((err) => {
					Msg.showErrorMsg(err.data.message);
				});
		},
	},
	components: {
		importFile,
	},
	beforeMount () {
		this.getContractTemp();
	},
};
</script>
<style lang="less" scoped>
.m-detailsFrom {
	/deep/ .el-form-item {
		margin-bottom: 10px;
		.el-form-item__content {
			.el-textarea.is-disabled {
				.el-textarea__inner {
					color: #333;
				}
			}
		}
	}
	.line-item {
		margin-bottom: 10px;
		label {
			width: 80px;
			text-align: right;
			vertical-align: middle;
			float: left;
			font-size: 14px;
			color: #606266;
			line-height: 30px;
			padding: 0 12px 0 0;
			box-sizing: border-box;
		}
		a {
			line-height: 30px;
			color: #409eff;
		}
	}
}
</style>
